package com.yimi.yinhepda.views.login.logic;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Spinner;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.base.BaseActivity2;
import com.pda.ym.callbacks.UICallBack;
import com.pda.ym.net.NetRequest;
import com.pda.ym.utils.Constants;
import com.pda.uce.commom.util.MyDialog;
import com.pda.ym.utils.SharedPreferUtil;
import com.pda.ym.utils.SpinnerData;
import com.yimi.yinhepda.views.login.contract.QueryCompanyContract;
import com.yimi.yinhepda.views.login.model.respones.CompanyFindAllResponse;
import com.yimi.yinhepda.views.login.presenter.QueryCompanyPresenter;

import java.util.List;

import static android.app.Activity.RESULT_OK;

/**
 * @author 000432
 */

public class QueryCompanyLogic extends UICallBack implements QueryCompanyContract.QueryCompanyBiz {

    private QueryCompanyPresenter mPresenter;

    public QueryCompanyLogic(QueryCompanyPresenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onError(String msg) {
        if (mPresenter.getReference() != null) {
            mPresenter.getReference().getCompanyInfoError(msg);
        }
    }

    @Override
    public void onSuccess(Object obj) {
        if (mPresenter.getReference() != null) {
            mPresenter.getReference().getCompanyInfoSuccess(obj);
        }
    }

    @Override
    public Context getContext() {
        return mPresenter.getContext();
    }

    @Override
    public void getCompanyInfo() {
        ((BaseActivity2) getContext()).showPgDlg("正在查询公司信息...");
        new NetRequest().setMethod(NetRequest.Method.GET).
                setResponsClazz(CompanyFindAllResponse.class).
                setUrl(Constants.QUERY_COMPANY_NAME).
                setConnTimeOut(10L * 1000).
                execute(this);
    }

    @Override
    public void saveData(List<SpinnerData> serverNameSpinnerList, Spinner serverSpinner, List<SpinnerData> companyNameSpinnerList,
                         Spinner companyNameSpinner) {
        //校验服务器地址
        if (serverNameSpinnerList == null || serverNameSpinnerList.isEmpty()) {
            return;
        }
        int position = serverSpinner.getSelectedItemPosition();
        SpinnerData serverInfo = serverNameSpinnerList.get(position);
        String serverIp = serverInfo.getValue();
        String serverName = serverInfo.getText();
        if (TextUtils.isEmpty(serverIp) || TextUtils.isEmpty(serverName)) {
            MyDialog.showAlertDialog(getContext(), getContext().getString(R.string.please_select_serverIp));
            return;
        }

        //校验公司名称
        if (companyNameSpinnerList == null || companyNameSpinnerList.isEmpty()) {
            return;
        }
        position = companyNameSpinner.getSelectedItemPosition();
        if (position < 0) {
            return;
        }
        SpinnerData companyInfo = companyNameSpinnerList.get(position);
        String companyCode = companyInfo.getValue();
        String companyName = companyInfo.getText();
        Log.d("queryname", companyCode + ":" + companyName);
        if (TextUtils.isEmpty(companyCode) || TextUtils.isEmpty(companyName)) {
            MyDialog.showAlertDialog(getContext(), getContext().getString(R.string.please_select_companyName));
            return;
        }
        //保存数据
        SharedPreferUtil.getInstance().setServerIP(serverIp);
        SharedPreferUtil.getInstance().setServerName(serverName);
        SharedPreferUtil.getInstance().setCompanyCode(companyCode);
        SharedPreferUtil.getInstance().setCompanyName(companyName);

        ((Activity) getContext()).setResult(RESULT_OK);
        ((Activity) getContext()).finish();
    }
}
